import moment from 'moment';


// 表单渲染适配器
export const setFieldsAdaptor = (data: BasicListApi.PageData) => {
    if (data.layout.tabs && data.dataSource) {
        data.layout.tabs.forEach((tab) => {
            tab.data.forEach((field) => {
                if (field.type === 'datetime') {
                    data.dataSource[field.key] = moment(data.dataSource[field.key]);
                }
            });
        });
        return data.dataSource;
    }
    return {};
};

// 表单提交前适配器（格式化数据）
export const submitFieldsAdaptor = (formValues: any) => {
    const result = formValues;
    Object.keys(formValues).forEach((key) => {
        if (moment.isMoment(formValues[key])) {
            result[key] = moment(formValues[key].format());
        }
    });
    return result;
};